package com.qf.web;


import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端数据
        try {
            User user = new User();
            Map<String, String[]> parameterMap = req.getParameterMap();
            BeanUtils.populate(user,parameterMap);
            UserService userService=new UserServiceImpl();
            //执行sql语句
            int a=userService.addUser(user);
            //添加完，查所有
            req.getRequestDispatcher("/findAll").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
